package com.yidu.hbatsft.service.impl;

import com.yidu.hbatsft.action.RivalWordAddData;
import com.yidu.hbatsft.domain.AtsBaseSport;
import com.yidu.hbatsft.domain.AtsRival;
import com.yidu.hbatsft.domain.AtsRivalResult;
import com.yidu.hbatsft.service.*;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.vo.UserVo;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AtsRivalWordServiceImpl{


    @Resource
    AtsEmpAthleteService atsEmpAthleteService;//运动员
    @Resource
    AtsEmpCoachService atsEmpCoachService;//教练员
    @Resource
    AtsEmpExpertService atsEmpExpertService;//专家
    @Resource
    AtsEmpLeaderService atsEmpLeaderService;//中心领导
    /*@Resource
    AtsDataDicSerivceImpl atsDataDicSerivce;//数据字典*/
    @Resource
    AtsBaseSportService atsBaseSportService;//运动项目
    @Resource
    RivalSerice atsRivalService;//主要对手
    @Resource
    RivalResultSerice atsRivalResultService;//主要对手

    public XWPFDocument createWord(int id, HttpServletResponse response, UserVo managerModel) throws IOException {
        RivalWordAddData rivalWordAddData = new RivalWordAddData();
        XWPFDocument document = null;
        AtsRival atsRival = atsRivalService.findbyid(id);
        List<AtsRivalResult> list = null;
        int count = 0;
        if(atsRival==null){
            return null;
        }else{
            System.out.println(atsRival.getSportid()+"--------------");
            AtsBaseSport atsBaseSport =  atsBaseSportService.selectByPrimaryKey(atsRival.getSportid());
            System.out.println("tiiiiii?:"+atsBaseSport.getSportName());
            atsRival.setSportName(atsBaseSport.getSportName());
            atsRival.setCreater(managerModel.getUsName());
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            atsRival.setCreatTime(sdf.format(date));//创建时间
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("对手："+atsRival.getRivName(),"UTF-8")+".docx");
            AtsRivalResult atsRivalResult = new AtsRivalResult();
            atsRivalResult.setRivId(id);
            list = atsRivalResultService.findAlls(atsRivalResult);
            count = atsRivalResultService.counts(atsRivalResult);
        }
        document = rivalWordAddData.exportWord(atsRival,list,count);
        return document;
    }

}
